package com.mallcloud.mall.app.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mallcloud.mall.app.api.entity.dto.AppCartDTO;
import com.mallcloud.mall.app.api.entity.vo.AppCartItemVO;
import com.mallcloud.mall.app.api.entity.vo.AppCartWithAmountVO;
import com.mallcloud.mall.app.mapper.AppCartMapper;
import com.mallcloud.mall.app.service.AppCartAdapterService;
import com.mallcloud.mall.app.service.AppShopCartItemService;
import com.mallcloud.mall.app.service.AppProductService;
import com.mallcloud.mall.common.core.exception.FailException;
import com.mallcloud.mall.common.data.tenant.TenantBroker;
import com.mallcloud.mall.common.data.tenant.TenantContextHolder;
import com.mallcloud.mall.common.security.util.SecurityUtils;
import com.mallcloud.mall.mall.api.entity.MallCartModel;
import com.mallcloud.mall.mall.service.MallProductAttrValueService;
import com.mallcloud.mall.product.api.entity.ShopCartItemModel;
import com.mallcloud.mall.product.api.entity.ShopDetailModel;
import com.mallcloud.mall.product.api.entity.SkuModel;
import com.mallcloud.mall.product.service.ShopDetailService;
import com.mallcloud.mall.product.service.SkuService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class AppCartServiceImpl extends ServiceImpl<AppCartMapper, ShopCartItemModel> implements AppShopCartItemService {
    private final SkuService skuService;
    private final AppCartAdapterService cartAdapterService;

    @Override
    public Long cartCount() {
        return this.count(Wrappers.<ShopCartItemModel>lambdaQuery()
                .eq(ShopCartItemModel::getUserId, SecurityUtils.getUser().getId())
        );
    }

    @Override
    public Boolean saveCartItem(AppCartDTO dto) {
        Long userId = SecurityUtils.getUser().getId();
        // 查询购物车已有商品
        ShopCartItemModel shopCartItem = this.getOne(Wrappers.<ShopCartItemModel>lambdaQuery()
                .eq(ShopCartItemModel::getSpuId, dto.getSpuId())
                .eq(ShopCartItemModel::getSkuId, dto.getSkuId())
                .eq(ShopCartItemModel::getUserId, userId)
        );
        if (Objects.nonNull(shopCartItem)) {
            this.update(Wrappers.<ShopCartItemModel>lambdaUpdate()
                    .eq(ShopCartItemModel::getSpuId, dto.getSpuId())
                    .eq(ShopCartItemModel::getSkuId, dto.getSkuId())
                    .set(ShopCartItemModel::getCount, shopCartItem.getCount() + dto.getCartNum())
            );
            return Boolean.TRUE;
        }
        SkuModel sku = skuService.getById(dto.getSkuId());
        if (Objects.isNull(sku)) {
            throw new FailException("sku不存在");
        }

        ShopCartItemModel shopCartItemSave = new ShopCartItemModel();
        shopCartItemSave.setShopId(TenantContextHolder.getTenantId());
        shopCartItemSave.setSpuId(dto.getSpuId());
        shopCartItemSave.setSkuId(dto.getSkuId());
        shopCartItemSave.setUserId(userId);
        shopCartItemSave.setCount(dto.getCartNum());
        shopCartItemSave.setPriceFee(sku.getPriceFee());
        shopCartItemSave.setIsChecked(true);
        return this.save(shopCartItemSave);
    }

    @Override
    public AppCartWithAmountVO info() {
        Long userId = SecurityUtils.getUser().getId();

        List<AppCartItemVO> cartItemList = this.baseMapper.getShopCartItems(userId, false, null);
        for (AppCartItemVO item : cartItemList) {
            item.setTotalAmount(item.getCount() * item.getSkuPriceFee());
        }
        if(CollectionUtil.isEmpty(cartItemList)){
            return null;
        }

        // 根据店铺ID划分item
        List<AppCartWithAmountVO.AppShopCartVO> shopCartList = cartAdapterService.conversionShopCart(cartItemList);

        AppCartWithAmountVO vo = new AppCartWithAmountVO();
        long total = 0L;
        for (AppCartItemVO cartItem : cartItemList) {
            if (Objects.equals(cartItem.getIsChecked(), 1)) {
                total += cartItem.getTotalAmount();
            }
        }
        vo.setShopCarts(shopCartList);
        vo.setTotalMoney(total);
        return vo;
    }
}
